﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using DAL;
using BLL;
namespace Web.Admin
{
    public partial class AuthorityAdd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
            //获取网址参数id
            object id = Request.QueryString["id"];
            if (id != null && !IsPostBack)
            {//修改
                //根据id从数据库取数据
                Authority o = AuthorityBll.SelectOne(int.Parse(id.ToString()));
                //显示到界面
                this.TextBox_name.Text = o.name;
                this.TextBox_Code.Text = o.Code;
                

            }

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            //第一步获取数据
            string name = this.TextBox_name.Text;
            string code = this.TextBox_Code.Text;
           
            //第二步  创建实体类对想
            Authority obj = new Authority();
            obj.name = name;
            obj.Code = code;
          
            //获取网址参数id
            object id = Request.QueryString["id"];
            if (id != null)
            {//修改
                obj.Id = int.Parse(id.ToString());//id赋值
                bool r = AuthorityBll.Update(obj);
                if (r)
                {//修改成功
                    Response.Redirect("/Admin/AuthorityList.aspx");
                }
                else
                {
                    Response.Write("<script>alert('修改失败')</script>");
                }
            }
            else//添加
            {
                //第三步调用 业务类的添加方法
                bool r = AuthorityBll.Add(obj);
                if (r)
                {//添加成功
                    Response.Redirect("/Admin/AuthorityList.aspx");
                }
                else
                {
                    Response.Write("<script>alert('添加失败')</script>");
                }
            }

        }
    }
}